﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using Models;
using Newtonsoft.Json;
using BLL;

namespace Web
{
    public partial class UserCart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies["Cart"] != null)//接收Cookie值
                {
                    DropDownList1.DataSource = LogisticsBLL.selectAll();
                    DropDownList1.DataTextField = "LogisticsName";
                    DropDownList1.DataValueField = "LogisticsID";
                    DropDownList1.DataBind();
                    Binder();
                }
            }
        }
        void Binder()
        {
            string Cart = Request.Cookies["Cart"].Value;
            List<CartItem> list = JsonConvert.DeserializeObject<List<CartItem>>(Cart);
            Repeater1.DataSource = list;
            Repeater1.DataBind();
            list.Clear();
        }
        protected void Button1_Click(object sender, EventArgs e)//删除
        {
            string Cart = Request.Cookies["Cart"].Value;
            List<CartItem> list = JsonConvert.DeserializeObject<List<CartItem>>(Cart);
            int GoodsID = Convert.ToInt32(((Button)sender).CommandArgument);
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].GoodsID == GoodsID)
                {
                    list.RemoveAt(i);
                }
            }
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string json = serializer.Serialize(list);
            HttpCookie httpCookie = new HttpCookie("Cart", json);
            httpCookie.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(httpCookie);
            Repeater1.DataSource = null;
            Binder();
            Response.Redirect("UserCart.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)//结算
        {
            if (Session["UserID"] != null)
            {
                if (Request.Cookies["Cart"] != null)
                {
                    string Cart = Request.Cookies["Cart"].Value;
                    string Logistics = DropDownList1.SelectedValue;
                    List<CartItem> list = JsonConvert.DeserializeObject<List<CartItem>>(Cart);
                    bool areturn = false;
                    for (int i = 0; i < list.Count; i++)
                    {
                        OrdersModels om = new OrdersModels();
                        om.UserID = int.Parse(Session["UserID"].ToString());
                        om.LogisticsID = int.Parse(Logistics);
                        om.OredersNum = list[i].Nums;
                        om.GoodsID = list[i].GoodsID;
                        om.LittlePrice = list[i].LittlePrice;

                        if (OrdersBLL.addOrder(om.UserID, om.LogisticsID, om.LittlePrice, om.OredersNum, om.GoodsID))
                        {
                            areturn = true;
                        }
                    }
                    if (areturn == true)
                    {
                        HttpCookie myCookie = new HttpCookie("Cart");
                        myCookie.Expires = DateTime.Now.AddDays(-1d);
                        Response.Cookies.Add(myCookie);
                        Response.Redirect("BuySuccessful.aspx");
                    }
                    else
                    {
                        Response.Write("<script>alert('购买失败！')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('购物车为空！')</script>");
                }
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }
    }

}
